package controllers;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import models.History;
import models.Result;
import models.RoleControl;
import models.User;
import monitor.Monitor;
import play.cache.Cache;
import play.libs.Codec;
import play.libs.Images;
import play.mvc.Before;
import play.mvc.Controller;
import utils.ExcleJson;
import utils.Result1;

import com.google.gson.Gson;

public class Application extends Controller {

	public static void json() {
		//DataInsert.insertData();
		String optionstr = ExcleJson.getTimeNumber();
		renderText(optionstr);
	}
	
	@Before(unless={"Application.captcha","Application.genRandomId"})
	static void gloabl(){
		Monitor.doing(request);
	}

	@Before(unless={"Application.captcha","Application.genRandomId"})
	static void checkAccess() {
		if (!session.contains("username")) {
			Secure.login();
		}
	}
	
	@Before(unless={"Application.captcha","Application.genRandomId"})
	public static void checkRole(){
		if(!"/".equals(request.url)){
			String controller=request.controller;
			String action=request.actionMethod;
			String value= connect().role.value;
			if(value!=null){
				int userPurview = 0;
				String[] roles = value.split("\\|");
				Map<String,Integer> map = new HashMap<String,Integer>();
				for(String _role : roles){
					if(_role.length()==1){
						break;
					}
					String[] purview =_role.split(":");
					if(purview[0].equalsIgnoreCase(controller)){
						userPurview = Integer.parseInt(purview[1]);
						break;
					}
				}
				
				List<RoleControl> list=RoleControl.find("controller = ?",controller).fetch();
				//if(list.isEmpty()) renderArgs.put("hasRole", true);
				//System.out.println("-----------------userPurview:"+userPurview);
				for (RoleControl role : list) {
					
					if(RoleControl.checkPower(userPurview,role.value)){
						//System.out.println("key you:"+ controller +" "+role.action);
						map.put(role.action, 1);
					}else{
						if(action.equalsIgnoreCase(role.action))//忽略大小写
							results2("错误","对不起，您没有权限!","/departs/blank",false);
						
						//System.out.println("key mei:"+ controller +" "+role.action);
						map.put(role.action, 0);
					}
				}
				renderArgs.put("roleMap", map);
			}
		}
	}
	
	@Before(unless={"Application.captcha","Application.genRandomId"})
	public static void global(){
		User user = connect();
		if(user != null && !request.isAjax()) {
			Map<String,String[]> keys = params.all();
			StringBuffer postText = new StringBuffer();
			Iterator<String> key = keys.keySet().iterator();
			while(key.hasNext()){
				String k = key.next();
				if(!k.equals("authenticityToken") && !k.equals("body") && !k.equals("controller") && !k.equals("action")){
					String b = params.get(k);
					postText.append("{[key:"+k+"] [value:"+b+"]}\r\n");
				}
			}
			History history = new History(user.truename,postText.toString());
			history.save();
		}
	}


	
	protected static User connect() {
		return User.findById(Long.parseLong(session.get("userid")));
	}
	
    public static void index() {
    	render();
    }
    
    public static void iframe_blank() {
		render();
	}
    
    protected static String getJSON(String jsonString) {
		return new Gson().toJson(jsonString);
	}
     
    public static void result(String title,String message,String url,boolean flag) {
    	set_default_result(title, message, url,flag);
		render("@result");
	}
    public static void result2(String title,String message,String url,boolean flag) {
    	set_default_result2(title, message, url,flag);
    	render("@result2");
    }
    
    protected static void renderCloseResult(String msg) { 
    	//String title = "安排班级成功！";
		Result1 result = new Result1(msg); 
		result.setMessage(msg); 
		result.addLink("关闭窗口", "javascript:closeWidRealod();"); 
		renderTemplate("result.html", result);
	}
    
    protected static void set_default_result(String title,String message,String url,boolean flag) {
		Result result = new Result(title,message);
		if(flag){
			result.addLink("继续添加", url);
		}
		result.addLink("关闭窗口","javascript:close_dialog_realod();");
		renderArgs.put("result", result);
	}
    protected static void set_default_result2(String title,String message,String url,boolean flag) {
    	Result result = new Result(title,message);
    	if(flag){
    		result.addLink("返回设置", url);
    	}
    	result.addLink("关闭窗口","javascript:close_dialog_realod();");
    	renderArgs.put("result", result);
    }
    
    public static void results(String title,String message,String url,boolean flag) {
    	set_default_results(title, message, url,flag);
		render("@result");
	}
    
    protected static void set_default_results(String title,String message,String url,boolean flag) {
		Result result = new Result(title,message);
		result.addLink("关闭窗口","javascript:close_dialog_realod();");
		renderArgs.put("result", result);
	}
    
    public static void results2(String title,String message,String url,boolean flag) {
    	set_default_results2(title, message, url,flag);
		render("@result2");
	}
    
    protected static void set_default_results2(String title,String message,String url,boolean flag) {
		Result result = new Result(title,message);
		result.addLink("返回","javascript:history.go(-1);");
		renderArgs.put("result", result);
	}
    
    protected static void set_tab(int tab){
		if(renderArgs.get("tab_index")==null) renderArgs.put("tab_index", tab);
	}
    
	
	public static void new_result(String title,String message,String url,boolean flag) {
    	set_new_result(title, message, url,flag);
		render("@result");
	}
    
    protected static void set_new_result(String title,String message,String url,boolean flag) {
		Result result = new Result(title,message);
		if(flag){
			result.addLink("返回", url);
		}
		renderArgs.put("result", result);
	}
    
    public static String getURLDecoder(String str) {
		try {
			if(str==null) return "";
			str = URLEncoder.encode(str, "iso8859-1");
			str = URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			str = "";
		}
		return str;
	}
    
	/**
	 * 验证码
	 * @param id
	 */
	public static void captcha(String randomID) {
		Images.Captcha captcha = Images.captcha(145,45);
		String code = captcha.getText("#49B07F",4);
		Cache.set(randomID, code, "30mn");
		renderBinary(captcha);
	}
	/**
	 * 验证码相关
	 * @return 一个不重复的id
	 */
	public static String genRandomId(){
		return Codec.UUID();
	}
    
}